package com.csy.property.controller.community;

import com.csy.commons.core.controller.BaseController;
import com.csy.commons.core.domain.BaseResponse;
import com.csy.commons.core.page.PageResult;
import com.csy.property.community.domain.AjCommunity;
import com.csy.property.community.domain.dto.AjCommunityDto;
import com.csy.property.community.domain.vo.AjCommunityVo;
import com.csy.property.community.service.AjCommunityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/community")
@Slf4j
public class AjCommunityController extends BaseController {

    @Resource
    private AjCommunityService ajCommunityService;
    /*
    * 多条件分页查询
    * */
    @GetMapping("/list")
    @PreAuthorize("@pe.hasPerms('system:community:list')")
    public PageResult list(AjCommunity ajCommunity){

        startPage();
        List<AjCommunityDto> list = ajCommunityService.queryList(ajCommunity);

        return getData(list);
    }

    /*
    * 新增小区
    * */
    @PostMapping
    public BaseResponse add(@RequestBody AjCommunity ajCommunity){

        return toAjax(ajCommunityService.insertAjCommunity(ajCommunity));
    }
    @GetMapping("/{communityId}")
    public BaseResponse getInfo(@PathVariable("communityId") Long communityId){
        return BaseResponse.success(ajCommunityService.selectAjCommunityById(communityId));
    }

    /*
    * 修改小区
    * */
    @PutMapping
    public BaseResponse edit(@RequestBody AjCommunity ajCommunity){
        return toAjax(ajCommunityService.updateAjCommunity(ajCommunity));
    }

    /*
     * 根据ID删除小区数据
     * */
    @DeleteMapping("/{communityIds}")
    public BaseResponse delete(@PathVariable Long[] communityIds){
        return toAjax(ajCommunityService.deleteAjCommunity(communityIds));
    }

    /*
    * 小区下拉列表展示
    * */
    @GetMapping("/queryPullDown")
    public BaseResponse queryPullDown(AjCommunity ajCommunity){

        //打印入参日志
        log.info("log() called with parameters => [ajCommunity = {}]",ajCommunity);
        List<AjCommunityVo> voList = null;
        try {
            voList = ajCommunityService.queryPullDown(ajCommunity);
        }catch (Exception e){
            log.warn("获取小区下拉列表失败！",e);
        }
        //打印日志 返回结果
        log.info("log() returned:{} ",voList);
        return BaseResponse.success(voList);
    }


}
